package kafka010;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.util.Arrays;
import java.util.Properties;

/**
 *
 *  /usr/java/jdk1.8.0_121/bin/java -cp code-kafka-0.10x-1.0-SNAPSHOT.jar com.archgeek.kafka010.KafkaConsumerTest
 *
 * @author pizhihui
 * @date 2021-08-19 18:23
 */
public class KafkaConsumerTest {


    public static void main(String[] args) {

        String topic = args[0];
        String gropId = args[1];


        // c13
        String brokers = "10.11.36.10:9092,10.11.36.58:9092,10.11.36.75:9092";
//        stable
//        String brokers = "10.9.22.23:9092,10.9.22.22:9092,10.9.22.25:9092";


        // /usr/java/jdk1.8.0_121/bin/java -cp code-kafka-0.10x-1.0-SNAPSHOT.jar com.archgeek.kafka010.KafkaConsumerTest C13_JG_unify_event_intergrate_parse_business_traffic_v4_topic t_business_client_demo_test


        Properties props = new Properties();
        props.put("bootstrap.servers", brokers);
        props.put("group.id", gropId);
        props.put("enable.auto.commit", "true");
        props.put("auto.commit.interval.ms", "1000");
//        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("key.deserializer", "org.apache.kafka.common.serialization.ByteArrayDeserializer");
//        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.ByteArrayDeserializer");
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");
        props.put("kafka.version", "0.10.2");
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
        consumer.subscribe(Arrays.asList(topic));
        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(100);
            for (ConsumerRecord<String, String> record : records) {
                System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
            }
        }


    }


}
